import axios from "../../axios/request";
import store from '../index'
export default {
    namespaced: true,
    state(){
        return {
            requests: [],
        }
    },

    mutations: {
        setRequests(state, requests){
            state.requests = requests
        },
        addRequest(state, request){
            state.requests.push(request);
            console.log('state', state.requests);
            console.log('state', request);
        }
    },

    actions:{
        async create({commit, dispatch}, payload) {
            try{
                const token = store.getters['auth/token'];
                const url = `https://vue-app-b1a15-default-rtdb.firebaseio.com/request.json?auth=${token}`;
                const {data} = await axios.post(url, payload);
                commit('addRequest', {...payload, id: data.name});
                dispatch('setMessage', {
                    value: 'Заявка успешно создана',
                    type: 'primary'
                }, {root: true})
            } catch (e) {
                dispatch('setMessage', {
                    value: e.message,
                    type: 'danger'
                }, {root: true})
            }
        },

        async load({commit, dispatch}) {
            try{
                const token = store.getters['auth/token'];
                const url = `https://vue-app-b1a15-default-rtdb.firebaseio.com/request.json?auth=${token}`;
                const {data} = await axios.get(url);
                const requests = Object.keys(data).map(id => ({... data[id], id}))
                commit('setRequests', requests);
            } catch (e) {
                dispatch('setMessage', {
                    value: e.message,
                    type: 'danger'
                }, {root: true})
            }
        },

        async loadById({commit, dispatch}, id) {
            try{
                const token = store.getters['auth/token'];
                const url = `https://vue-app-b1a15-default-rtdb.firebaseio.com/request/${id}.json?auth=${token}`;
                const {data} = await axios.get(url);
                return data
            } catch (e) {
                dispatch('setMessage', {
                    value: e.message,
                    type: 'danger'
                }, {root: true})
            }
        },

        async remove({commit, dispatch}, id) {
            try{
                const token = store.getters['auth/token'];
                const url = `https://vue-app-b1a15-default-rtdb.firebaseio.com/request/${id}.json?auth=${token}`;
                await axios.delete(url);
                dispatch('setMessage', {
                    value: 'Заявка удалена',
                    type: 'primary'
                }, {root: true})
            } catch (e) {
                dispatch('setMessage', {
                    value: e.message,
                    type: 'danger'
                }, {root: true})
            }
        },

        async update({commit, dispatch}, request) {
            try{
                const token = store.getters['auth/token'];
                const url = `https://vue-app-b1a15-default-rtdb.firebaseio.com/request/${request.id}.json?auth=${token}`;
                await axios.put(url, request);
                dispatch('setMessage', {
                    value: 'Заявка обновлена',
                    type: 'primary'
                }, {root: true})
            } catch (e) {
                dispatch('setMessage', {
                    value: e.message,
                    type: 'danger'
                }, {root: true})
            }
        }
    },

    getters:{
        requests(state){
            return state.requests;
        }
    }
}
